<?php

namespace app\mall_back\model;

use think\admin\Model;


/**
 * @property string $id
 * @property string $goods_id
 * @property string $unit_price
 * @property string $people_number
 * @property string $participants_number
 * @property string $residue_number
 * @property string $choice_robots
 * @property string $sequence_no
 * @property string $sequence_total
 * @property string $sequence_current
 * @property string $draw_delay
 * @property string $appoint_phone
 * @property string $winning_user_id
 * @property string $award_number
 * @property string $is_draw_a_lottery
 * @property string $status
 * @property string $is_hot
 * @property string $win_time
 * @property string $up_time
 * @property string $create_time
 */
class GoodsLuckyDrawModel extends Model
{
    const STATE_PENDING  = 0;
    const STATE_STARTING = 1;
    const STATE_DRAWING  = 2;
    const STATE_FINISHED = 3;

    const STATE_TEXTS = [
        GoodsLuckyDrawModel::STATE_PENDING  => '预售中',
        GoodsLuckyDrawModel::STATE_STARTING => '进行中',
        GoodsLuckyDrawModel::STATE_DRAWING  => '待开奖',
        GoodsLuckyDrawModel::STATE_FINISHED => '已开奖'
    ];

    protected $table = 'one_goods_lucky_draw';

    public function user()
    {
        return $this->belongsTo('app\data\model\DataUser', 'winning_user_id', 'id', [], 'LEFT')->field('id,phone,headimg,nickname');
    }

    public function goods()
    {
        return $this->belongsTo('app\mall_back\model\GoodsModel', 'goods_id', 'id', [], 'LEFT')->field('id,goods_name,cover');
    }

    /**
     * @param $state
     *
     * @return string
     */
    public static function getStatusText($state)
    {
        return static::STATE_TEXTS[$state] ?? '未知状态';
    }

    /**
     * @param array $edata
     *
     * @return int|true
     */
    public static function createSubLuckyDraw(array $edata = [])
    {
        $result  = TRUE;
        $templet = $edata;
        unset($templet['id']);
        unset($templet['update_time']);

        $batchCreateSeq              = $edata['sequence_total'] - 1;
        $templet['sequence_current'] = 1;
        while ($batchCreateSeq > 0) {
            $templet['sequence_current']++;
            $templet['status'] = 0;
            $result            &= static::insert($templet);
            $batchCreateSeq--;
        }

        return $result;
    }
}